#include "global.h"

void kclose(int fd)
{
	if ((fd > -1) && (fd < NUM_FILE_DESC))
	{
		File* f = running->fileDescriptors[fd];
		--f->refCount;
		
		if (f->mode == READ_PIPE)
		{
			--f->pipePtr->numReader;
		}
		else if (f->mode == WRITE_PIPE)
		{
			--f->pipePtr->numWriter;
		}
		
		if (f->refCount == 0)
		{
			if ((f->mode == READ_PIPE) || (f->mode == WRITE_PIPE))
			{
				f->pipePtr->status = PIPE_FREE;
				f->pipePtr = 0;
			}
		}
		running->fileDescriptors[fd] = 0;
	}
}
